package jku.semtech.ui;

import java.util.Map;
import java.util.Set;

import javax.faces.event.ValueChangeEvent;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

import jku.semtech.common.Player;
import jku.semtech.common.Team;
import jku.semtech.rdf.TeamRDF;

public class FrontendBean {
	private String selectedTeam = "";
	private static Set<String> teamList;
	private String selectedPlayer = "";
	private static Set<String> playerList;
	private Map<String, Team> teamMap;
	private Map<String, Player> playerMap;

	public FrontendBean() {
		teamMap = new TeamRDF().getTeams();
		teamList = teamMap.keySet();
	}

	public Set<String> getTeamList() {
		return teamList;
	}

	public void setSelectedTeam(String t) {
		this.selectedTeam = t;
	}

	public String getSelectedTeam() {
		return selectedTeam;
	}

	public Set<String> getPlayerList() {
		return playerList;
	}

	public void setSelectedPlayer(String p) {
		this.selectedPlayer = p;
	}

	public String getSelectedPlayer() {
		return selectedPlayer;
	}

	public String getSelectedTeamManager() {
		if (!selectedTeam.equals("")) {
			return teamMap.get(selectedTeam).getManagerName();
		} else {
			return "";
		}
	}

	public String getSelectedTeamNick() {
		if (!selectedTeam.equals("")) {
			return teamMap.get(selectedTeam).getNickName();
		} else {
			return "";
		}
	}

	public String getSelectedPlayerName() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getName();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerAbstract() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getDescription();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerPic() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getPicURL();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerDoB() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getBirthDate();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerPosition() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getFieldposition().toString();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerRealPosition() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getRealFieldPosition();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerCoB() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getBirthPlaceName();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerClub() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getClubName();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerClubManager() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getClubManagerName();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerClubSince() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getClubEntryYear();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String getSelectedPlayerGoals() {
		if (!selectedPlayer.equals("")) {
			Player temp = playerMap.get(selectedPlayer);
			if (temp != null) {
				return temp.getAverageGoals();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	//method needed for bar chart
	public CartesianChartModel getGoalStatistic() {
		CartesianChartModel c = new CartesianChartModel();

		//iterate over each team and fill model with chart series
		for(Team t : teamMap.values()) {
			ChartSeries x = new ChartSeries();
			x.setLabel(t.getTeamName());
			x.set(" ", t.getAllGoals());
			c.addSeries(x);
		}

		return c;
	}	
	
	public void teamChangedListener(ValueChangeEvent e) {
		System.out.println(e.getNewValue().toString());
		// assign new value to localeCode
		playerMap = teamMap.get(e.getNewValue().toString()).getPlayers();
		playerList = playerMap.keySet();
	}

}
